<script setup lang="ts">
import { onMounted, ref } from 'vue'
import SwiperHeader from './components/swiper-header.vue'
import { doGetEnablePageByType } from '@/apis/decoration/platform'
import { useAppStore } from '@/store/modules/app'
import { useUserStore } from '@/store/modules/user'
import { useSettingStore } from '@/store/modules/setting'
import { doPostBinding } from '@/apis/distribute/index'
import { lodingType, dataItem } from './types'
import type { PageInfo } from '@/apis/decoration/platform/model'
const endpointType = ref('H5_APP')

interface SwiperRef {
  chooseIndex: number
  setSwiperLoaded: (index: number) => void
}
const localList = ref<PageInfo>([])
const { doRequest } = useMember()
const { SET_LOADING } = useSettingStore()
const swiperRef = ref<SwiperRef | null>(null)
const $props = defineProps({
  code: {
    type: String,
    default: '',
  },
})

onMounted(async () => {
  initFn()
  initShare()
  initLocal(true)
})

const renderData = ref<dataItem[]>([
  {
    list: [],
    status: '加载中',
    name: '关注',
    active: false,
  },
  {
    list: [],
    status: '加载中',
    name: '推荐',
    active: false,
  },
])
async function initDecoration(index: number) {
  let list: any[] = []
  let status: lodingType = '加载中'
  if (renderData.value[index].active) return
  try {
    // #ifdef MP-WEIXIN
    endpointType.value = 'WECHAT_MINI_APP'
    // #endif
    // #ifndef MP-WEIXIN
    endpointType.value = 'H5_APP'
    // #endif
    const { code, data } = await doGetEnablePageByType(endpointType.value, 'RECOMMENDED_MALL_HOME_PAGE')
    if (code !== 200) {
      return uni.showToast({
        icon: 'none',
        title: '获取装修信息失败',
      })
    }
    list = JSON.parse(data.properties) || []
    status = list.length ? '成功' : '未配置'
  } catch (error) {
    status = '失败'
  } finally {
    swiperRef.value?.setSwiperLoaded(swiperRef.value.chooseIndex)
  }
  renderData.value[index] = { ...renderData.value[index], list, status, active: true }
}

async function initLocal(init = false) {
  let list: any[] = []
  let status: lodingType = '加载中'
  if (renderData.value[renderData.value.length - 1]['active']) return
  try {
    // #ifdef MP-WEIXIN
    endpointType.value = 'WECHAT_MINI_APP'
    // #endif
    // #ifndef MP-WEIXIN
    endpointType.value = 'H5_APP'
    // #endif
    const { code, data } = await doGetEnablePageByType(endpointType.value, 'SAME_CITY_MALL_HOME_PAGE')
    if (code !== 200 && !init) {
      return uni.showToast({
        icon: 'none',
        title: '获取装修信息失败',
      })
    }
    list = JSON.parse(data.properties) || []
    status = JSON.parse(data.properties).length ? '成功' : '未配置'
  } catch (error) {
    status = '失败'
  } finally {
    SET_LOADING(false)
    if (!init) swiperRef.value?.setSwiperLoaded(swiperRef.value.chooseIndex)
  }
  if (init) {
    renderData.value.push({
      list,
      status,
      name: '同城',
      active: false,
    })
    return
  } else {
    renderData.value[renderData.value.length - 1]['active'] = true
  }
}
async function initShare() {
  if (useAppStore().GET_PLUGIN('addon-distribute') && $props.code) {
    let code = decodeURIComponent($props.code)
    const { code: state, msg } = await doPostBinding(code)
    if (state !== 200) {
      useUserStore().SET_DIS_CODE(code)
      return uni.showToast({ title: `${msg ? msg : '绑定分销商失败'}`, icon: 'none' })
    }
    uni.showToast({ title: `绑定分销商成功`, icon: 'none' })
    useUserStore().DEL_DIS_CODE()
  }
}

function initFn(ind = 1) {
  doRequest()
  ind === 2 ? initLocal() : initDecoration(ind)
}
</script>
<template>
  <SwiperHeader ref="swiperRef" :o2o-show="!!localList.length" :render-data="renderData" @init-fn="initFn" />
</template>
